#include "Triangle.h"

Triangle::Triangle (Point* point1, Point* point2, Point* point3)
{
    //intializes all the variables
    this->point1 = point1;
    this->point2 = point2;
    this->point3 = point3;
	this->neighbour12 = NULL;
	this->neighbour23 = NULL;
	this->neighbour31 = NULL;
    this->exit = false;
    this->entrance = false;
}

Triangle::~Triangle()
{
}

double Triangle::getArea()
{
    //Used Heron's Formula of calculating Area.
    //got the length of the edges then used formula as shown below
    double line1 = point1->getDistanceFrom(point2);
    double line2 = point1->getDistanceFrom(point3);
    double line3 = point2->getDistanceFrom(point3);
    double s = (line1 + line2 + line3)/2;
    double Area = sqrt(s * (s-line1) * (s-line2) * (s-line3));
    return Area;
}

bool Triangle::isExit()
{
        return exit;
}

bool Triangle::isEntrance()
{
        return entrance;
}

bool Triangle::contains(Point* point){
    //checks if this point is one of the points in the triangle
	return point==point1||point==point2||point==point3;
}

Point* Triangle::getPoint1()
{
    return this -> point1;
}

Point* Triangle::getPoint2()
{
    return this -> point2;
}

Point* Triangle::getPoint3()
{
    return this -> point3;
}

Triangle* Triangle::getNeighbourBetween12()
{
    return neighbour12;
}

Triangle* Triangle::getNeighbourBetween23()
{
    return neighbour23;
}

Triangle* Triangle::getNeighbourBetween31()
{
    return neighbour31;
}

void Triangle::setNeighbourBetween12(Triangle* t)
{
    neighbour12 = t;
}

void Triangle::setNeighbourBetween23(Triangle* t)
{
    neighbour23 = t;
}

void Triangle::setNeighbourBetween31(Triangle* t)
{
    neighbour31 = t;
}


bool Triangle::assignNeighbour(Triangle* t){
        if (t == NULL){
                return false;
        }

        Point* a = t->getPoint1();
        Point* b = t->getPoint2();
        Point* c = t->getPoint3();

        //Checks if the points 1,2 1,3 and 2,3 are in the
        if (ifTwoOutOfThree(point1, point2, a, b, c)) {
			setNeighbourBetween12(t);
        } else if (ifTwoOutOfThree(point2, point3, a, b, c)){
			setNeighbourBetween23(t);
        } else if (ifTwoOutOfThree(point3, point1, a, b, c)){
			setNeighbourBetween31(t);
        } else {
			return false;
		}

        return true;
}


bool Triangle::ifTwoOutOfThree(Point* point1, Point* point2, Point* a, Point* b, Point* c){
        if (point1 == a){
                return ifOneOutOfTwo(point2, b, c);
        } else if (point1 == b){
                return ifOneOutOfTwo(point2, a, c);
        } else if (point1 == c){
                return ifOneOutOfTwo(point2, a, b);
        }
        return false;
}

bool Triangle::ifOneOutOfTwo(Point* point1, Point* a, Point* b){
    //Checks if either point a or b is equal to point1
        return point1 == a || point1 == b;
}

void Triangle::print(){

	//prints each point used for testing
        cout << "point1: ";
        point1->print();
        cout << "point2: ";
        point2->print();
        cout << "point3: ";
        point3->print();
}

//Jason's new functions
Triangle::DelaunayStatus Triangle::getDelaunayStatus (void)
{
        return delaunayStatus;
}
void Triangle::setDelaunayStatus (DelaunayStatus newStatus)
{
        delaunayStatus=newStatus;
}

void Triangle::setPoint1(Point *p) {point1 = p;}
void Triangle::setPoint2(Point *p) {point2 = p;}
void Triangle::setPoint3(Point *p) {point3 = p;}

